<link rel="stylesheet" type="text/css" href="../stylesheet/style.css"/>
<?php
/*
* @author Nguyen Xuan Hong
* @copyright 2011
*/

require_once('../includes/servicetype.tbl.php');
require_once('../includes/service.tbl.php');
require_once('../includes/servicenews.tbl.php');

$id = (int)sget('id');
$service_id = (int)sget('service');
if($mode == 'listservicetype' || $mode == 'editservicetype')
{
	$pClsServiceType = new clsServiceTypeTable();
	
	if($mode == 'listservicetype')
	{
		if($pClsServiceType->avaibleSubmit())
		{
			$pClsServiceType->loadBuffer();
			$pClsServiceType->insertDB();
			echo acpredirect("Đã thêm Loại dịch vụ");
			exit();
		}
		echo $pClsServiceType->createForm('Thêm Loại dịch vụ');
	}
	else if($mode == 'editservicetype')
	{
		if($pClsServiceType->avaibleSubmit())
		{
			$pClsServiceType->loadBuffer();
			$pClsServiceType->updateDB('id',$id);
			echo acpredirect("Đã cập nhật Loại dịch vụ", 'page='.$page.'&mode=listservicetype');
			exit();
		}
		$pClsServiceType->loadDB('id',$id);
		echo $pClsServiceType->createForm("Sửa thông tin Loại dịch vụ '".$pClsServiceType->value('name')."'");
	}
	
	createHeaderForm('Danh sách Loại dịch vụ');
	$list_service_type = $pClsServiceType->listobj();
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstservicetype',array("STT","Tên Loại dịch vụ","Sửa","Xóa"));
	$i = 0;
	if($list_service_type)
	{
		foreach($list_service_type as $servicetype)
		{
			$i++;
			$html_edit = '<a href="?page='.$page.'&mode=editservicetype&id='.$servicetype->value('id').'">Sửa</a>';	
			$html_delete = '<a href="?page='.$page.'&mode=deleteservicetype&id='.$servicetype->value('id').'">Xóa</a>';
			$table->addRow($i, array($i, $servicetype->value('name'), $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deleteservicetype')
{		
	// Redefine insert
	$pClsServiceType = new clsServiceTypeTable($id);
	if($_POST['submit'])
	{
		$pClsServiceType->removeDB('id', $id);
		echo acpredirect('Đã xoá Loại dịch vụ', 'page='.$page.'&mode=listservicetype');
		exit();
	}
	else if($_POST['nosubmit'])
	{
		echo acpredirect('Trở về', 'page='.$page.'&mode=listservicetype');
		exit();
	}
	
	createDeleteForm("Loại dịch vụ '".$pClsServiceType->value('name')."'");   
	exit();
}
//------------------------------------------------------------------------------------------

if($mode == 'listservice' || $mode == 'editservice')
{
	$pClsService = new clsServiceTable();
	$pClsServiceType = new clsServiceTypeTable();
    $pClsService->attList['servicetype_id']['list'] = $pClsServiceType->cbolist();
	
	if($mode == 'listservice')
	{
		if($pClsService->avaibleSubmit())
		{
			$pClsService->loadBuffer();
			$pClsService->insertDB();
			echo acpredirect("Đã thêm Dịch vụ");
			exit();
		}
		echo $pClsService->createForm('Thêm Dịch vụ');
	}
	else if($mode == 'editservice')
	{
		if($pClsService->avaibleSubmit())
		{
			$pClsService->loadBuffer();
			$pClsService->updateDB('id',$id);
			echo acpredirect("Đã cập nhật Dịch vụ", 'page='.$page.'&mode=listservice');
			exit();
		}
		$pClsService->loadDB('id',$id);
		echo $pClsService->createForm("Sửa thông tin Dịch vụ '".$pClsService->value('name')."'");
	}
	
	createHeaderForm('Danh sách Dịch vụ');
	$list_service = $pClsService->listobj();
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstservice',array("STT","Tên Giải đấu","DS Bản tin","Sửa","Xóa"));
	$i = 0;
	if($list_service)
	{
		foreach($list_service as $service)
		{
			$i++;
			$html_news = '<a href="?page='.$page.'&service='.$service->value('id').'&mode=listservicenews">Tin tức</a>';	
			$html_edit = '<a href="?page='.$page.'&mode=editservice&id='.$service->value('id').'">Sửa</a>';	
			$html_delete = '<a href="?page='.$page.'&mode=deleteservice&id='.$service->value('id').'">Xóa</a>';
			$table->addRow($i, array($i, $service->value('name'), $html_news, $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deleteservice')
{		
	// Redefine insert
	$pClsService = new clsServiceTable($id);
	if($_POST['submit'])
	{
		$pClsService->removeDB('id', $id);
		echo acpredirect('Đã xoá Dịch vụ', 'page='.$page.'&mode=listservice');
		exit();
	}
	else if($_POST['nosubmit'])
	{
		echo acpredirect('Trở về', 'page='.$page.'&mode=listservice');
		exit();
	}
	
	createDeleteForm("Dịch vụ '".$pClsService->value('name')."'");   
	exit();
}

//------------------------------------------------------------------------------------------

if($mode == 'listservicenews' || $mode == 'editservicenews')
{
	$pClsServiceNews = new clsServiceNewsTable();
	if($mode == 'listservicenews')
	{
		if($pClsServiceNews->avaibleSubmit())
		{
			$pClsServiceNews->loadBuffer();
			$pClsServiceNews->setvalue('service_id', $service_id);
			$pClsServiceNews->setvalue('keyword', convert_url($pClsServiceNews->value('title')));
			$pClsServiceNews->insertDB();
			
			$id = $pClsServiceNews->value('id');
   		
			$image = upload_file("Upload/", 'imgbrowser_image', $pClsServiceNews->value('image'), $id);
			if($image != "")
			{
				$pClsServiceNews->setvalue('image', $image);
			} 
			
			$pClsServiceNews->updateDB('id',$id);
			
			echo acpredirect("Đã thêm Bản tin dịch vụ");
			exit();
		}
		echo $pClsServiceNews->createForm('Thêm Bản tin dịch dụ');
	}
	else if($mode == 'editservicenews')
	{
		if($pClsServiceNews->avaibleSubmit())
		{
			$pClsServiceNews->loadBuffer();
			$pClsServiceNews->setvalue('keyword', convert_url($pClsServiceNews->value('title')));
			
			$image = upload_file("Upload/", 'imgbrowser_image', $pClsServiceNews->value('image'), $id);
			if($image != "")
			{
        		$pClsServiceNews->setvalue('image', $image);
			} 
			
			$pClsServiceNews->updateDB('id',$id);
			echo acpredirect("Đã cập nhật Bản tin dịch vụ", 'page='.$page.'&service='.$service_id.'&mode=listservicenews');
			exit();
		}
		$pClsServiceNews->loadDB('id',$id);
		echo $pClsServiceNews->createForm("Sửa bản tin Dịch vụ '".$pClsServiceNews->value('name')."'");
	}
	
	$pClsService = new clsServiceTable($service_id);
	createHeaderForm('Danh sách Bản tin dịch vụ '.$pClsService->value('name'));
	$list_servicenews = $pClsServiceNews->listobj($service_id, '', 100);
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstservicenews',array("STT","Bản tin","Sửa","Xóa"));
	$i = 0;
	if($list_servicenews)
	{
		foreach($list_servicenews as $servicenews)
		{
			$i++;
			$html_edit = '<a href="?page='.$page.'&service='.$service_id.'&mode=editservicenews&id='.$servicenews->value('id').'">Sửa</a>';	
			$html_delete = '<a href="?page='.$page.'&service='.$service_id.'&mode=deleteservicenews&id='.$servicenews->value('id').'">Xóa</a>';
			
			$table->addRow($i, array($i, $servicenews->value('title'), $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deleteservicenews')
{		
	// Redefine insert
	$pClsServiceNews = new clsServiceNewsTable($id);
	if($_POST['submit'])
	{
		$pClsServiceNews->removeDB('id', $id);
		echo acpredirect('Đã xoá Bản tin dịch vụ', 'page='.$page.'&service='.$service_id.'&mode=listservicenews');
		exit();
	}
	else if($_POST['nosubmit'])
	{
		echo acpredirect('Trở về', 'page='.$page.'&service='.$service_id.'&mode=listservicenews');
		exit();
	}
	
	createDeleteForm("Bản tin dịch vụ '".$pClsServiceNews->value('name'));   
	exit();
}

